package com.gusti.turnos.controller.rest;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gusti.turnos.form.AppointmentForm;
import com.gusti.turnos.form.UserForm;
import com.gusti.turnos.model.User;
import com.gusti.turnos.response.rest.TurnosResponse;
import com.gusti.turnos.services.IAppointmentService;
import com.gusti.turnos.services.IEventService;
import com.gusti.turnos.services.IUserService;
import com.gusti.turnos.util.MessageUtil;
import com.gusti.turnos.util.ServerUtil;

/**
 * 
 * @author gusti
 *
 */
@Controller
public class RestController {

	@Autowired
	IUserService userService;
	@Autowired
	IAppointmentService appointmentService;
	@Autowired
	IEventService eventService;

	//Users
	@RequestMapping(value="/retrieve/users", method=RequestMethod.GET)
	@ResponseBody
	public TurnosResponse retrieveUsers( HttpServletRequest request ) {

		return userService.retrieveUsers( getSessionId( request ), getUserUuid( request ) );
	}

	@RequestMapping(value="/save/user", method=RequestMethod.POST)
	@ResponseBody
	public TurnosResponse saveUser( @Valid UserForm userForm, BindingResult result, HttpServletRequest request ) {

		TurnosResponse turnosResponse = new TurnosResponse();

		if( result.hasErrors() ) {

			turnosResponse.setData( result.getFieldErrors() );
			return turnosResponse;
		}

		turnosResponse = userService.saveUser( getSessionId( request ), getUserUuid( request ), userForm );

		//clean the user password here cause in service the method is transactional jeje
		if( turnosResponse.isSuccess() ) {
			((User)turnosResponse.getData()).setPassword( null );
		}

		return turnosResponse;
	}

	@RequestMapping(value="/delete/user", method=RequestMethod.POST)
	@ResponseBody
	public TurnosResponse deleteUser( String id, HttpServletRequest request ) {

		return userService.deleteUser( getSessionId( request ), getUserUuid( request ), id );
	}

	//Appointments
	@RequestMapping(value="/retrieve/appointments", method=RequestMethod.GET)
	@ResponseBody
	public TurnosResponse retrieveAppointments( HttpServletRequest request ) {

		return appointmentService.retrieveAppointments( getSessionId( request ), getUserUuid( request ) );
	}

	@RequestMapping(value="/save/appointment", method=RequestMethod.POST)
	@ResponseBody
	public TurnosResponse saveAppointment( @Valid AppointmentForm appointmentForm, BindingResult result, HttpServletRequest request ) {

		TurnosResponse turnosResponse = new TurnosResponse();

		if( result.hasErrors() ) {
			turnosResponse.setData( result );
			return turnosResponse;
		}

		return appointmentService.saveAppointment( getSessionId( request ), getUserUuid( request ), appointmentForm );
	}

	@RequestMapping(value="/delete/appointment", method=RequestMethod.POST)
	@ResponseBody
	public TurnosResponse deleteAppointment( String id, HttpServletRequest request ) {

		return appointmentService.deleteAppointment( getSessionId( request ), getUserUuid( request ), id );
	}

//	//AppointmentEvents
//	@RequestMapping(value="/retrieve/appointmentEvents", method=RequestMethod.GET)
//	@ResponseBody
//	public TurnosResponse retrieveAppointmentEvents( @PathVariable("sessionId") String sessionId, @PathVariable("uuid") String uuid ) {
//
//		return eventService.retrieveAppointmentEvents( sessionId, uuid );
//	}
//
//	@RequestMapping(value="/{sessionId}/{uuid}/save/appointmentEvent", method=RequestMethod.POST)
//	@ResponseBody
//	public TurnosResponse saveAppointmentEvent( @PathVariable("sessionId") String sessionId, @PathVariable("uuid") String uuid ) {
//
//		return eventService.newAppointmentEvent( sessionId, uuid );
//	}
//
//	@RequestMapping(value="/{sessionId}/{uuid}/delete/appointmentEvent", method=RequestMethod.POST)
//	@ResponseBody
//	public TurnosResponse deleteAppointmentEvent( @PathVariable("sessionId") String sessionId, @PathVariable("uuid") String uuid ) {
//
//		return eventService.deleteAppointmentEvent( sessionId, uuid );
//	}
//
//	//BirthdayEvents
//	@RequestMapping(value="/{sessionId}/{uuid}/retrieve/birthdayEvents", method=RequestMethod.GET)
//	@ResponseBody
//	public TurnosResponse retrieveBirthdayEvents( @PathVariable("sessionId") String sessionId, @PathVariable("uuid") String uuid ) {
//		
//		return eventService.retrieveBirthdayEvents( sessionId, uuid );
//	}
//
//	@RequestMapping(value="/{sessionId}/{uuid}/save/birthdayEvent", method=RequestMethod.POST)
//	@ResponseBody
//	public TurnosResponse saveBirthdayEvent( @PathVariable("sessionId") String sessionId, @PathVariable("uuid") String uuid ) {
//		
//		return eventService.newBirthdayEvent( sessionId, uuid );
//	}
//
//	@RequestMapping(value="/{sessionId}/{uuid}/delete/birthdayEvent", method=RequestMethod.POST)
//	@ResponseBody
//	public TurnosResponse deleteBirthdayEvent( @PathVariable("sessionId") String sessionId, @PathVariable("uuid") String uuid ) {
//		
//		return eventService.deleteBirthdayEvent( sessionId, uuid );
//	}

	@RequestMapping(value="/404", method=RequestMethod.GET)
	@ResponseBody
	public TurnosResponse error404() {
		TurnosResponse messageResponse = new TurnosResponse();
		messageResponse.setErrorMessage( MessageUtil.getMessage( MessageUtil.ERROR_404 ) );
		return messageResponse;
	}

	@RequestMapping(value="/500", method=RequestMethod.GET)
	@ResponseBody
	public TurnosResponse error500() {
		TurnosResponse messageResponse = new TurnosResponse();
		messageResponse.setErrorMessage( MessageUtil.getMessage( MessageUtil.ERROR_500 ) );
		return messageResponse;
	}

	private String getSessionId( HttpServletRequest request ) {
		HttpSession session = request.getSession(false);
		return session.getId();
	}

	private String getUserUuid( HttpServletRequest request ) {
		HttpSession session = request.getSession(false);
		if( session.getAttribute( ServerUtil.CURRENT_USER ) != null ) {
			return ((User) session.getAttribute( ServerUtil.CURRENT_USER )).getUuid();
		}
		return null;
	}
}
